package org.agilelang.db.sync;

/*
 AgileLang - A fourth generation language(http://www.agilelang.org).

 Copyright (C) 2013 Roderick Cusirramos (roderick20@hotmail.com)

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import java.awt.Color;
import java.awt.Component;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeCellRenderer;

public class CellRenderer implements TreeCellRenderer {

    JLabel label = new JLabel(" ");
    JPanel renderer = new JPanel();
    DefaultTreeCellRenderer defaultRenderer = new DefaultTreeCellRenderer();
    Color backgroundSelectionColor;
    Color backgroundNonSelectionColor;
    private Icon icon_table = new ImageIcon(getIconImage("org/agilelang/db/sync/table.png"));
    private Icon icon_attr = new ImageIcon(getIconImage("org/agilelang/db/sync/attr.png"));
    private Icon icon_model = new ImageIcon(getIconImage("org/agilelang/db/sync/model.png"));

    public CellRenderer() {
        renderer.add(label);        
        backgroundSelectionColor = defaultRenderer.getBackgroundSelectionColor();
        backgroundNonSelectionColor = defaultRenderer.getBackgroundNonSelectionColor();
    }

    static public Image getIconImage(String urlimagen) {
        Image retValue = Toolkit.getDefaultToolkit().
                getImage(ClassLoader.getSystemResource(urlimagen));
        return retValue;
    }

    public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected,
            boolean expanded, boolean leaf, int row, boolean hasFocus) {
        Component returnValue = null;
        if ((value != null) && (value instanceof DefaultMutableTreeNode)) {
            Object dataObject = ((DefaultMutableTreeNode) value).getUserObject();
            if (dataObject instanceof NodeData) {
                NodeData data = (NodeData) dataObject;

                if (data.getNodeType().equals("table")) {
                    label.setIcon(icon_table);                    
                    if (data.getNewOld().equals("new")) {
                        label.setForeground(Color.RED);
                        label.setText(data.getName()+"(new)");
                    } else {
                        label.setForeground(Color.BLACK);
                        label.setText(data.getName());
                    }
                }
                if (data.getNodeType().equals("column")) {
                    label.setIcon(icon_attr);
                    label.setText(data.getName());
                     if (data.getNewOld().equals("new")) {
                        label.setForeground(Color.RED);
                        label.setText(data.getName()+"(type = "+data.getType()+", new)");
                    } else if (data.getNewOld().equals("modify")) {
                        label.setForeground(Color.darkGray);
                        label.setText(data.getName()+"(type = "+data.getType()+", modify)");
                    }else{
                        label.setForeground(Color.BLACK);
                        label.setText(data.getName()+"(type = "+data.getType()+")");
                    }
                }
                if (data.getNodeType().equals("head")) {
                    label.setIcon(icon_model);
                    label.setText(data.getName());
                    if (data.getNewOld().equals("new")) {
                        label.setForeground(Color.RED);
                    } else {
                        label.setForeground(Color.BLACK);
                    }
                }
                renderer.setEnabled(tree.isEnabled());
                returnValue = renderer;
            }
        }


        if (selected) {
            renderer.setBackground(backgroundSelectionColor);
        } else {
            renderer.setBackground(backgroundNonSelectionColor);
        }
        
        
        if (returnValue == null) {
            returnValue = defaultRenderer.getTreeCellRendererComponent(tree,
                    value, selected, expanded, leaf, row, hasFocus);
        }
        return returnValue;

    }
}
